package com.itheima.ai.controller;

import com.itheima.ai.config.service.ChatHistoryRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;

//智能客服
@RequiredArgsConstructor
@RestController
@RequestMapping("/ai")
public class CustomerServiceController {
    private final ChatClient CustomerChatClient; //选用智能客服的Bean

    private final ChatHistoryRepository chatHistoryRepository;

    @RequestMapping(value = "/service", produces = "text/html;charset=utf-8")
    public Flux<String> service(String prompt, String chatId) { //流式输出
        // 1.保存会话id
        chatHistoryRepository.save("service", chatId);
        // 2.请求模型
        return CustomerChatClient.prompt()
                .user(prompt)
                .advisors(a -> a.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId))
                .stream()
                .content();
    }

//    @RequestMapping(value = "/service2", produces = "text/html;charset=utf-8")
//    public String service2(String prompt, String chatId) { //非流式输出
//        // 1.保存会话id
//        chatHistoryRepository.save("service", chatId);
//        // 2.请求模型
//        return kefuChatClient.prompt()
//                .user(prompt)
//                .advisors(a -> a.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId))
//                .call()
//                .content();
//    }
}
